//
//  CustomTextField.swift
//  YoruNeko
//
//  Created by hgc on 2021/6/25.
//

import Foundation
import UIKit
import SwiftUI

//struct CustomTextField: UIViewRepresentable {
//
//    class Coordinator: NSObject, UITextFieldDelegate {
//
//        @Binding var text: String
//        var didBecomeFirstResponder = false
//
//        init(text: Binding<String>) {
//            _text = text
//        }
//
//        func textFieldDidChangeSelection(_ textField: UITextField) {
//            text = textField.text ?? ""
//        }
//
//    }
//
//    @Binding var text: String
//    var defaultText: String
//    var isFirstResponder: Bool = false
//    var isEnabled: Bool = true
//    var alignment: NSTextAlignment = .left
//
//    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
//        let textField = UITextField(frame: .zero)
//        textField.placeholder = defaultText
//        textField.isUserInteractionEnabled = isEnabled
//        textField.textAlignment = alignment
//        textField.autocapitalizationType = .none
//        textField.autocorrectionType = .no
//        textField.delegate = context.coordinator
//        return textField
//    }
//
//
//    func makeCoordinator() -> CustomTextField.Coordinator {
//        return Coordinator(text: $text)
//    }
//
//    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
//        uiView.text = text
//        if isFirstResponder && !context.coordinator.didBecomeFirstResponder  {
//            uiView.becomeFirstResponder()
//            context.coordinator.didBecomeFirstResponder = true
//        }
//    }
//}

struct CustomTextField: UIViewRepresentable {
    
    class Coordinator: NSObject, UITextFieldDelegate {
        
        @Binding var text: String
        //      @Binding var nextResponder : Bool?
        //      @Binding var isResponder : Bool?
        
        
        init(text: Binding<String>) {
            _text = text
            //        _isResponder = isResponder
            //        _nextResponder = nextResponder
        }
        
        func textFieldDidChangeSelection(_ textField: UITextField) {
            text = textField.text ?? ""
        }
        
        //      func textFieldDidBeginEditing(_ textField: UITextField) {
        //         DispatchQueue.main.async {
        //             self.isResponder = true
        //         }
        
        
        func textFieldDidEndEditing(_ textField: UITextField) {
            //         DispatchQueue.main.async {
            //             self.isResponder = false
            //             if self.nextResponder != nil {
            //                 self.nextResponder = true
            //             }
            //         }
        }
    }
    
    
    @Binding var text: String
    //  @Binding var nextResponder : Bool?
    //  @Binding var isResponder : Bool?
    
    var isSecured : Bool = false
    var defaultText: String
    var keyboard : UIKeyboardType
    var width:CGFloat

    
    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        let f = textField.frame
        textField.frame = CGRect.init(x: f.minX, y: f.minY, width: self.width, height: f.height)
        textField.returnKeyType = UIReturnKeyType.next //表示继续下一步
        textField.clearButtonMode = .unlessEditing  //编辑时不出现，编辑后才出现清除按钮
        textField.textAlignment = .center
        textField.isSecureTextEntry = isSecured
        textField.placeholder = defaultText
        textField.autocapitalizationType = .none
        textField.autocorrectionType = .no
        textField.keyboardType = keyboard
        textField.delegate = context.coordinator
        textField.isUserInteractionEnabled =  true
        
        textField.becomeFirstResponder()
        return textField
    }
    
    init(text:Binding<String>,defaultText:String,keyboardType:UIKeyboardType,width:CGFloat){
        _text = text
        self.defaultText = defaultText
        self.keyboard = keyboardType
        self.width = width
    }
    
    func makeCoordinator() -> CustomTextField.Coordinator {
        return Coordinator(text: $text)
    }
    
    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
        uiView.text = text
        //       if isResponder ?? false {
        //           uiView.becomeFirstResponder()
        //       }
    }
    
    
}




